package com.ruoyi.web.controller.project.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.controller.project.mapper.TOrderRecordMapper;
import com.ruoyi.web.controller.project.domain.TOrderRecord;
import com.ruoyi.web.controller.project.service.ITOrderRecordService;

import javax.validation.Validator;

/**
 * 订单记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-27
 */
@Slf4j
@Service
public class TOrderRecordServiceImpl implements ITOrderRecordService 
{
    @Autowired
    private TOrderRecordMapper tOrderRecordMapper;

    /**
     * 查询订单记录
     * 
     * @param recordId 订单记录主键
     * @return 订单记录
     */
    @Override
    public TOrderRecord selectTOrderRecordByRecordId(String recordId)
    {
        return tOrderRecordMapper.selectTOrderRecordByRecordId(recordId);
    }

    /**
     * 查询订单记录列表
     * 
     * @param tOrderRecord 订单记录
     * @return 订单记录
     */
    @Override
    public List<TOrderRecord> selectTOrderRecordList(TOrderRecord tOrderRecord)
    {
        return tOrderRecordMapper.selectTOrderRecordList(tOrderRecord);
    }

    /**
     * 新增订单记录
     * 
     * @param tOrderRecord 订单记录
     * @return 结果
     */
    @Override
    public int insertTOrderRecord(TOrderRecord tOrderRecord)
    {
        return tOrderRecordMapper.insertTOrderRecord(tOrderRecord);
    }

    /**
     * 修改订单记录
     * 
     * @param tOrderRecord 订单记录
     * @return 结果
     */
    @Override
    public int updateTOrderRecord(TOrderRecord tOrderRecord)
    {
        return tOrderRecordMapper.updateTOrderRecord(tOrderRecord);
    }

    /**
     * 批量删除订单记录
     * 
     * @param recordIds 需要删除的订单记录主键
     * @return 结果
     */
    @Override
    public int deleteTOrderRecordByRecordIds(String[] recordIds)
    {
        return tOrderRecordMapper.deleteTOrderRecordByRecordIds(recordIds);
    }

    /**
     * 删除订单记录信息
     * 
     * @param recordId 订单记录主键
     * @return 结果
     */
    @Override
    public int deleteTOrderRecordByRecordId(String recordId)
    {
        return tOrderRecordMapper.deleteTOrderRecordByRecordId(recordId);
    }

    @Autowired
    protected Validator validator;

    @Override
    public String importRecord(List<TOrderRecord> tOrderRecordList, boolean updateSupport, String operName, Long userId)
    {
        if (StringUtils.isNull(tOrderRecordList) || tOrderRecordList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TOrderRecord record : tOrderRecordList)
        {
            try
            {
                TOrderRecord selectedRecord = tOrderRecordMapper.selectTOrderRecordByRecordDateAndUserId(record.getRecordDate(), userId);
                if (selectedRecord == null){
                    TOrderRecord recordTemp = new TOrderRecord();
                    recordTemp.setRecordDate(record.getRecordDate());
                    if (record.getIncomeAmount()!=null){
                        recordTemp.setIncomeAmount(record.getIncomeAmount());
                    }else{
                        recordTemp.setIncomeAmount(BigDecimal.ZERO);
                    }
                    if (record.getOrderCount()!=null){
                        recordTemp.setOrderCount(record.getOrderCount());
                    }else{
                        recordTemp.setOrderCount("0");
                    }
                    if (record.getVehicleCount()!=null){
                        recordTemp.setVehicleCount(record.getVehicleCount());
                    }else{
                        recordTemp.setVehicleCount("0");
                    }
                    recordTemp.setCreatedAt(new Date());
                    recordTemp.setUserId(userId);
                    tOrderRecordMapper.insertTOrderRecord(recordTemp);
                    successNum++;
                    successMsg.append("<br/> " + successNum + "条记录" + " 导入成功");
                }else{
//                    log.debug("已存在该记录 selectedRecord={}",selectedRecord);

                    if (selectedRecord.getIncomeAmount().compareTo(record.getIncomeAmount()) == 0 &&
                    selectedRecord.getOrderCount().equals(record.getOrderCount()) &&
                    selectedRecord.getVehicleCount().equals(record.getVehicleCount())){
                        log.debug("数据相同");
                        successNum++;
                        successMsg.append("<br/> " + successNum + "条记录" + " 比对相同!");
                    }else{
                        TOrderRecord recordTemp = new TOrderRecord();
                        recordTemp.setRecordDate(record.getRecordDate());
                        recordTemp.setUserId(userId);
                        recordTemp.setIncomeAmount(record.getIncomeAmount());
                        recordTemp.setOrderCount(record.getOrderCount());
                        recordTemp.setVehicleCount(record.getVehicleCount());
                        tOrderRecordMapper.updateTOrderRecordByDateAndUserId(recordTemp);
                        log.debug("更新数据");
                        successNum++;
                        successMsg.append("<br/> " + successNum + "条记录" + " 更新导入成功");
                    }
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> " + failureNum + "条记录" + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public HashMap<String, Object> getTotal(TOrderRecord tOrderRecord) {
        return tOrderRecordMapper.getTotal(tOrderRecord);
    }
}
